package com.xt.base.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * Title:
 * </p>
 * 
 * <p>
 * Description:ʱ�䴦�?����
 * </p>
 * 
 * <p>
 * Company:
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2006
 * </p>
 * 
 * <p>
 * Author: Hongbo,Yang
 * </p>
 * 
 * <p>
 * Version: 1.0
 * </p>
 * 
 * <p>
 * Create Date:Jun 8, 2006
 * </p>
 */
@SuppressWarnings("unchecked")
public class DateUtils {

	public final static String YYYY = "yyyy";

	public final static String MM = "MM";

	public final static String DD = "dd";

	public final static String YYYY_MM_DD = "yyyy-MM-dd";

	public final static String YYYY_MM = "yyyy-MM";

	public final static String HH_MM_SS = "HH:mm:ss";

	public final static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

	public static String formatStr_yyyyMMddHHmmssS = "yyyy-MM-dd HH:mm:ss.S";

	public static String formatStr_yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";

	public static String formatStr_yyyyMMddHHmm = "yyyy-MM-dd HH:mm";

	public static String formatStr_yyyyMMddHH = "yyyy-MM-dd HH";

	public static String formatStr_yyyyMMdd = "yyyy-MM-dd";

	public static String[] formatStr = { formatStr_yyyyMMddHHmmss,
			formatStr_yyyyMMddHHmm, formatStr_yyyyMMddHH, formatStr_yyyyMMdd };

	public static String endDateString = "  23:59:59";

	/**
	 * ���캯��
	 */
	public DateUtils() {
	}

	/**
	 * ���ڸ�ʽ������<code>Date</code>���͵����ڸ�ʽ��Ϊ<code>String</code>��
	 * 
	 * @param date
	 *            ���ʽ��������
	 * @param pattern
	 *            ʱ����ʽ
	 * @return һ������ʽ���˵�<code>String</code>����
	 */
	public static String format(Date date, String pattern) {
		if (date == null)
			return "";
		else
			return getFormatter(pattern).format(date);
	}

	/**
	 * Ĭ�ϰ����ڸ�ʽ����yyyy-mm-dd��ʽ
	 * 
	 * @param date
	 * @return
	 */
	public static String format(Date date) {
		if (date == null)
			return "";
		else
			return getFormatter(YYYY_MM_DD).format(date);
	}

	/**
	 * ���ַ�����Ĭ��ת��Ϊyyyy-mm-dd��ʽ��Data����
	 * 
	 * @param strDate
	 * @return
	 */
	public static Date format(String strDate) {
		Date d = null;
		if (strDate == "")
			return null;
		else
			try {
				d = getFormatter(YYYY_MM_DD).parse(strDate);
			} catch (ParseException pex) {
				return null;
			}
		return d;
	}

	/**
	 * ���ڽ�������<code>String</code>���͵����ڽ���Ϊ<code>Date</code>��
	 * 
	 * @param date
	 *            ���ʽ��������
	 * @param pattern
	 *            ������ʽ
	 * @exception ParseException
	 *                ��������ַ��ܱ�������һ������
	 * @return һ������ʽ���˵�<code>Date</code>����
	 */
	public static Date parse(String strDate, String pattern)
			throws ParseException {
		try {
			return getFormatter(pattern).parse(strDate);
		} catch (ParseException pe) {
			throw new ParseException(
					"Method parse in Class DateUtils  err: parse strDate fail.",
					pe.getErrorOffset());
		}
	}

	/**
	 * ��ȡ��ǰ����
	 * 
	 * @return һ���������յ�<code>Date</code>������
	 */
	public static synchronized Date getCurrDate() {
		Calendar calendar = Calendar.getInstance();
		return calendar.getTime();
	}

	/**
	 * ��ȡ��ǰ����
	 * 
	 * @return һ���������յ�<code>String</code>�����ڣ�������ʱ���롣yyyy-mm-dd
	 */
	public static String getCurrDateStr() {
		return format(getCurrDate(), YYYY_MM_DD);
	}

	/**
	 * ��ȡ��ǰʱ��
	 * 
	 * @return һ����������ʱ�����<code>String</code>�����ڡ�hh:mm:ss
	 */
	public static String getCurrTimeStr() {
		return format(getCurrDate(), HH_MM_SS);
	}

	/**
	 * ��ȡ��ǰ����ʱ��,��ʽ: yyyy��MM��dd hh:mm:ss
	 * 
	 * @return һ����������ʱ�����<code>String</code>�����ڡ�yyyy-MM-dd hh:mm:ss
	 */
	public static String getCurrDateTimeStr() {
		return format(getCurrDate(), YYYY_MM_DD_HH_MM_SS);
	}

	/**
	 * �õ�����ʱ��ĺ����ơ�
	 * @param date Date��������ƶ�ʱ�䡣
	 * @return ������ʱ�䣻long�͡�
	 */
	public static long getCurrDateTimeStr(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.getTimeInMillis();
	}

	/**
	 * ��ȡ��ǰ��� ��ʽ��yyyy
	 * 
	 * @return ��ǰ���
	 */
	public static String getYear() {
		return format(getCurrDate(), YYYY);
	}

	/**
	 * ��ȡ��ǰ�·� ��ʽ��MM
	 * 
	 * @return ��ǰ�·�
	 */
	public static String getMonth() {
		return format(getCurrDate(), MM);
	}

	/**
	 * ��ȡ�������ڵ��·� ��ʽ��MM
	 * @param date java.util.Date������ʱ�䡣
	 * @return ����ʱ����·�
	 */
	public static String getMonth(Date date) {
		return format(date, MM);
	}

	/**
	 * ��ȡ��ǰ���ں� ��ʽ��dd
	 * 
	 * @return ��ǰ���ں�
	 */
	public static String getDay() {
		return format(getCurrDate(), DD);
	}

	/**
	 * ����������ʽ�жϸ��ַ��Ƿ�Ϊ�Ϸ��������
	 * 
	 * @param strDate
	 *            Ҫ�жϵ�����
	 * @param pattern
	 *            ������ʽ
	 * @return true ����ǣ����򷵻�false
	 */
	public static boolean isDate(String strDate, String pattern) {
		try {
			parse(strDate, pattern);
			return true;
		} catch (ParseException pe) {
			return false;
		}
	}

	/**
	 * �жϸ��ַ��Ƿ�Ϊ�ض���ʽ���ڣ�������������yyyy-MM-dd�����
	 * 
	 * @param strDate
	 *            Ҫ�жϵ�����
	 * @return true ����ǣ����򷵻�false
	 */
	 public static boolean isDate(String strDate) {
		 try {
			 parse(strDate, YYYY_MM_DD);
			 return true;
		 }
		 catch (ParseException pe) {
			 return false;
		 }
	 }
	/**
	 * �жϸ��ַ��Ƿ�Ϊ�ض���ʽ��ݣ���ʽ��yyyy�����
	 * @param strDate Ҫ�жϵ�����
	 * @return true ����ǣ����򷵻�false
	 */
	public static boolean isYYYY(String strDate) {
		try {
			parse(strDate, YYYY);
			return true;
		} catch (ParseException pe) {
			return false;
		}
	}

	/**
	 * �жϸ��ַ��Ƿ�Ϊ�ض���ʽ��ݣ���ʽ��yyyy-MM�����
	 * @param strDate  Ҫ�жϵ�����
	 * @return true ����ǣ����򷵻�false
	 */
	public static boolean isYYYY_MM(String strDate) {
		try {
			parse(strDate, YYYY_MM);
			return true;
		} catch (ParseException pe) {
			return false;
		}
	}

	/**
	 * �жϸ��ַ��Ƿ�Ϊ�ض���ʽ�������գ���ʽ��yyyy-MM-dd�����
	 * 
	 * @param strDate
	 *            Ҫ�жϵ�����
	 * @return true ����ǣ����򷵻�false
	 */
	public static boolean isYYYY_MM_DD(String strDate) {
		try {
			parse(strDate, YYYY_MM_DD);
			return true;
		} catch (ParseException pe) {
			return false;
		}
	}

	/**
	 * �жϸ��ַ��Ƿ�Ϊ�ض���ʽ������ʱ���루��ʽ��yyyy-MM-dd HH:mm:ss�����
	 * 
	 * @param strDate
	 *            Ҫ�жϵ�����
	 * @return true ����ǣ����򷵻�false
	 */
	public static boolean isYYYY_MM_DD_HH_MM_SS(String strDate) {
		try {
			parse(strDate, YYYY_MM_DD_HH_MM_SS);
			return true;
		} catch (ParseException pe) {
			return false;
		}
	}

	/**
	 * �жϸ��ַ��Ƿ�Ϊ�ض���ʽʱ���루��ʽ��HH:mm:ss�����
	 * 
	 * @param strDate
	 *            Ҫ�жϵ�����
	 * @return true ����ǣ����򷵻�false
	 */
	public static boolean isHH_MM_SS(String strDate) {
		try {
			parse(strDate, HH_MM_SS);
			return true;
		} catch (ParseException pe) {
			return false;
		}
	}

	/**
	 * �жϸ��ַ��Ƿ�Ϊ�ض���ʽʱ�䣨������ʱ����hh:mm:ss�����
	 * 
	 * @param strTime
	 *            Ҫ�жϵ�ʱ��
	 * @return true ����ǣ����򷵻�false
	 */
	 public static boolean isTime(String strTime) {
		 try {
			 parse(strTime, HH_MM_SS);
			 return true;
		 }
		 catch (ParseException pe) {
			 return false;
		 }
	 }
	/**
	 * �жϸ��ַ��Ƿ�Ϊ�ض���ʽ����ʱ�䣨������������ʱ���� yyyy-MM-dd hh:mm:ss�����
	 * 
	 * @param strDateTime
	 *            Ҫ�жϵ�����ʱ��
	 * @return true ����ǣ����򷵻�false
	 */
	 public static boolean isDateTime(String strDateTime) {
		 try {
			 parse(strDateTime, YYYY_MM_DD_HH_MM_SS);
			 return true;
		 }
		 catch (ParseException pe) {
			 return false;
		 }
	 }
	/**
	 * ��ȡһ���򵥵����ڸ�ʽ������
	 * 
	 * @return һ���򵥵����ڸ�ʽ������
	 */
	private static SimpleDateFormat getFormatter(String parttern) {
		return new SimpleDateFormat(parttern);
	}

	/**
	 * �õ������º������
	 * 
	 * @param refDate
	 * @param monthNum
	 * @return java.util.Date
	 */
	public static Date nextDate(String refenceDate, int monthNum) {
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Date dRef = null, theday = null;
		try {
			dRef = formatter.parse(refenceDate);
			theday = nextDate(dRef, monthNum);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return theday;
	}

	/**
	 * �õ������º������
	 * 
	 * @param refDate
	 * @param monthNum
	 * @return java.util.Date
	 */
	public static Date nextDate(Date refDate, int monthNum) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(refDate);
		cal.add(Calendar.MONTH, monthNum);
		return cal.getTime();
	}

	/**
	 * ��ȡ����ǰ�ĺ�intevalDay�������
	 * 
	 * @param refenceDate
	 *            �����ڣ���ʽΪ��yyyy-MM-dd��
	 * @param intevalDays
	 *            �������
	 * @return ����������
	 */
	public static String getNextDate(String refenceDate, int intevalDays) {
		try {
			return getNextDate(parse(refenceDate, YYYY_MM_DD), intevalDays);
		} catch (Exception ee) {
			return "";
		}
	}

	/**
	 * ��ȡ����ǰ�ĺ�intevalDay�������
	 * 
	 * @param refenceDate
	 *            Date ������
	 * @param intevalDays
	 *            int �������
	 * @return String ����������
	 */
	public static String getNextDate(Date refenceDate, int intevalDays) {
		try {
			java.util.Calendar calendar = java.util.Calendar.getInstance();
			calendar.setTime(refenceDate);
			calendar.set(Calendar.DATE, calendar.get(Calendar.DATE)
					+ intevalDays);
			return format(calendar.getTime(), YYYY_MM_DD);
		} catch (Exception ee) {
			return "";
		}
	}

	/**
	 * �õ���������֮��ĺ�����ʱ�䡣
	 * @param startDate java.lang.String����ʼʱ�䡣
	 * @param endDate   java.lang.String��������ʱ�䡣
	 * @return ������ʱ�䡣
	 */
	public static long getIntevalDays(String startDate, String endDate) {
		try {
			return getIntevalDays(parse(startDate, YYYY_MM_DD), parse(endDate,
					YYYY_MM_DD));
		} catch (Exception ee) {
			return 0l;
		}
	}

	/**
	 * �õ���������֮��ĺ�����ʱ�䡣
	 * @param startDate java.uti.Date����ʼʱ�䡣
	 * @param endDate   java.uti.Date��������ʱ�䡣
	 * @return ������ʱ�䡣
	 */
	public static long getIntevalDays(Date startDate, Date endDate) {
		try {
			java.util.Calendar startCalendar = java.util.Calendar.getInstance();
			java.util.Calendar endCalendar = java.util.Calendar.getInstance();
			startCalendar.setTime(startDate);
			endCalendar.setTime(endDate);
			long diff = endCalendar.getTimeInMillis()
					- startCalendar.getTimeInMillis();
			return (diff / (1000 * 60 * 60 * 24));
		} catch (Exception ee) {
			return 0l;
		}
	}

	/**
	 * ���ַ����ڵĸ�ʽת��Ϊjava.util.Date��
	 * @param dateTimeStr java.lang.String���ַ����ڡ�
	 * @return �ַ����ڶ�Ӧ��java.util.Date���ڡ�
	 */
	public static Date parseToDate(String dateTimeStr) {
		if (dateTimeStr == null) return null;
		Date d = null;
		int formatStrLength = formatStr.length;
		for (int i = 0; i < formatStrLength; i++) {
			d = parseToDate2(dateTimeStr, formatStr[i]);
			if (d != null) {
				break;
			}
		}
		return d;
	}

	/**
	 * ���ƶ��ĸ�ʽ���ַ�����ת��ΪDate���͵����ڡ�
	 * @param dateTimeStr String�����ڡ�
	 * @param formatString String�����ڸ�ʽ��
	 * @return ��Ӧ�ַ����ڵ�java.util.Date�������ڡ�
	 */
	private static Date parseToDate2(String dateTimeStr, String formatString) {
		Date d = null;
		SimpleDateFormat sdf = new SimpleDateFormat(formatString);
		try {
			d = sdf.parse(dateTimeStr);
		} catch (ParseException pe) {
			d=null;
		}
		return d;
	}

	/** ȡ��ָ���µ������� 
	 *  @param date java.util.Date�����ڡ�
	 *  @return ������
	 * */
	public static int getDayCountOfMonth(Date date) {
		// LastDayOfMonth ldm = new LastDayOfMonth();
		// ldm.setThisYear(Integer.parseInt(DateUtils.format(date, "yyyy")));
		return getLastDayOfMonth(Integer.parseInt(DateUtils.format(date, "MM")));
	}

	/** ȡ��ָ���µ������� 
	 *  @param date String�����ڡ�
	 *  @return ������
	 * */
	public static int getDayCountOfMonth(String date) {
		return getDayCountOfMonth(DateUtils.format(date));
	}

	/** ȡ�õ��µ������� 
	 *  @return ������ 
	 * */
	public static int getDayCountOfMonth() {
		Calendar calendar = Calendar.getInstance();
		return getDayCountOfMonth(calendar.getTime());
	}

	/** ȡ�õ��µ��������ڣ���String���鷵�ء� */
	public static String[] getAllDayOfMonth(String format) {
		String date[] = null;
		Calendar calendar = Calendar.getInstance();
		date = getAllDayOfMonth(calendar.getTime(), format);
		return date;
	}

	/** ȡ��ָ���µ��������ڣ���String���鷵�ء� */
	public static String[] getAllDayOfMonth(Date date, String format) {
		String strDate[] = null;
		Date date2[] = getAllDayOfMonth(date);
		strDate = new String[date2.length];
		for (int i = 0; i < date2.length; i++) {
			strDate[i] = DateUtils.format(date2[i], format);
		}
		return strDate;
	}

	/** ȡ�õ��µ��������ڡ���Date���鷵�ء� */
	public static Date[] getAllDayOfMonth() {
		Calendar calendar = Calendar.getInstance();
		return getAllDayOfMonth(calendar.getTime());
	}

	/** ȡ��ָ���µ��������ڡ���Date���鷵�ء� */
	public static Date[] getAllDayOfMonth(Date date) {
		String strDate[] = null;
		Date dateR[] = null;
		int day = getDayCountOfMonth(date);
		String yearMonth = DateUtils.format(date, "yyyy-MM");
		strDate = new String[day];
		dateR = new Date[day];
		for (int i = 0; i < day; i++) {
			strDate[i] = new StringBuffer(yearMonth).append("-").append(
					String.valueOf(i + 1)).toString();
			dateR[i] = DateUtils.format(strDate[i]);
		}
		return dateR;
	}
	
	/**��ȡ��������֮����ꡣ
	 * 
	 * @param startDate java.util.Date����ʼ���ڡ�
	 * @param endDate   java.util.Date����ֹ���ڡ�
	 * @return String[]���洢�Ÿ�ʽΪ��yyyy�������ڵ����顣
	 * @throws Exception 
	 */
	public static String[] getBetweenYear(String startDate, String endDate) {
		
		if(startDate.compareTo(endDate)>0){
			String temp = endDate;
			endDate = startDate;
			startDate = temp;
		}	
		
		Date date1 = format(startDate);		
		Date date2 = format(endDate);		
		
		int year = Integer.parseInt(format(date1,YYYY));
		int month = Integer.parseInt(format(date1,MM))-1;
		int dayOfMonth = Integer.parseInt(format(date1,DD));		
		GregorianCalendar gc1 = new GregorianCalendar(year,month,dayOfMonth);
		
		int yearCount = 1+Math.abs(
			Integer.parseInt(format(date2,YYYY))-Integer.parseInt(format(date1,YYYY)));
		
		String dateArr[] = new String[yearCount];
		dateArr[0] = format(gc1.getTime(),YYYY);
		for(int i=1;i<yearCount;i++){
			gc1.add(Calendar.YEAR, 1);
			dateArr[i] = format(gc1.getTime(),YYYY);
		}
		
		return dateArr;
	}

	
	/**��ȡ��������֮����¡�
	 * 
	 * @param startDate java.util.Date����ʼ���ڡ�
	 * @param endDate   java.util.Date����ֹ���ڡ�
	 * @return String[]���洢�Ÿ�ʽΪ��yyyy-MM�������ڵ����顣
	 * @throws Exception 
	 */
	public static String[] getBetweenMonth(String startDate, String endDate) {
		
		if(startDate.compareTo(endDate)>0){
			String temp = endDate;
			endDate = startDate;
			startDate = temp;
		}	
		
		Date date1 = format(startDate);		
		Date date2 = format(endDate);		
		
		int year = Integer.parseInt(format(date1,YYYY));
		int month = Integer.parseInt(format(date1,MM))-1;
		int dayOfMonth = Integer.parseInt(format(date1,DD));		
		GregorianCalendar gc1 = new GregorianCalendar(year,month,dayOfMonth);
		
		int monthCount = 1+Math.abs(
			(Integer.parseInt(format(date2,YYYY))-Integer.parseInt(format(date1,YYYY)))*12
			+(Integer.parseInt(format(date2,MM))-Integer.parseInt(format(date1,MM))));
		
		String dateArr[] = new String[monthCount];
		dateArr[0] = format(gc1.getTime(),YYYY_MM);
		for(int i=1;i<monthCount;i++){
			gc1.add(Calendar.MONTH, 1);
			dateArr[i] = format(gc1.getTime(),YYYY_MM);
		}
		
		return dateArr;
	}
	
	/**��ȡ��������֮����ܡ�
	 * 
	 * @param startDate java.util.Date����ʼ���ڡ�
	 * @param endDate   java.util.Date����ֹ���ڡ�
	 * @return String[]���洢�Ÿ�ʽΪ��yyyy�������ڵ����顣
	 * @throws Exception 
	 */
	public static String[] getBetweenWeek(String startDate, String endDate) {
		
		if(startDate.compareTo(endDate)>0){
			String temp = endDate;
			endDate = startDate;
			startDate = temp;
		}	
		
		Date date1 = format(startDate);		
		Date date2 = format(endDate);		
		
		int year = Integer.parseInt(format(date1,YYYY));
		int month = Integer.parseInt(format(date1,MM))-1;
		int dayOfMonth = Integer.parseInt(format(date1,DD));		
		GregorianCalendar gc1 = new GregorianCalendar(year,month,dayOfMonth);
		
		year = Integer.parseInt(format(date2,YYYY));
		month = Integer.parseInt(format(date2,MM))-1;
		dayOfMonth = Integer.parseInt(format(date2,DD));
		GregorianCalendar gc2 = new GregorianCalendar(year,month,dayOfMonth);
		
		gc1.setFirstDayOfWeek(GregorianCalendar.MONDAY);
		gc2.setFirstDayOfWeek(GregorianCalendar.MONDAY);
		
		//System.out.print(gc2.get(Calendar.WEEK_OF_YEAR)+";"+gc1.get(Calendar.WEEK_OF_YEAR));
		int gc2Week=gc2.get(Calendar.WEEK_OF_YEAR);
		int gc1Week=gc1.get(Calendar.WEEK_OF_YEAR);
		int weekCount=gc2Week<gc1Week?52-gc1Week+1:gc2Week-gc1Week+1;
		//System.out.println(";"+weekCount);
		
		String dateArr[] = new String[weekCount];
		dateArr[0] = gc1.get(Calendar.WEEK_OF_YEAR)<10?"0"+String.valueOf(gc1.get(Calendar.WEEK_OF_YEAR))+"��":String.valueOf(gc1.get(Calendar.WEEK_OF_YEAR))+"��";
		for(int i=1;i<weekCount;i++){
			gc1.add(Calendar.WEEK_OF_YEAR, 1);
			dateArr[i] = gc1.get(Calendar.WEEK_OF_YEAR)<10?"0"+String.valueOf(gc1.get(Calendar.WEEK_OF_YEAR))+"��":String.valueOf(gc1.get(Calendar.WEEK_OF_YEAR))+"��";
		}
		
		return dateArr;
	}

	/**��ȡ��������֮����������ڡ�
	 * 
	 * @param startDate java.util.Date����ʼ���ڡ�
	 * @param endDate   java.util.Date����ֹ���ڡ�
	 * @return Date[]���������顣
	 */
	public static Date[] getBetweenDate(String startDate, String endDate) {
		
		Date date1 = null;
		Date date2 = null;
		Date date3 = null;		
		List listDate = new ArrayList();
		try {
			date2 = format(endDate);
			date1 = format(startDate);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		date3 = format(startDate);
		Date dateArr[] = null;
		Date dateResult[] = null;
		int count = 0;

		while (date2.compareTo(date3) >= 0) {
			dateArr = getAllDayOfMonth(date3);

			date3 = new Date(dateArr[dateArr.length - 1].getTime());
			date3.setDate(dateArr.length+1);
			
			count += dateArr.length;
			listDate.add(dateArr);
		}
		
		int sizeList = listDate.size();
		int sizeArr  = 0;
		dateResult   = new Date[count];
		for(int i=0,k=0;i<sizeList;i++){
			dateArr = (Date[])listDate.get(i);
			sizeArr = dateArr.length;
			
			for(int j=0;j<sizeArr;j++){
				if(dateArr[j].compareTo(date1)<0){
					continue;
				}
				else if(dateArr[j].compareTo(date2)>0){
					break;
				}
				
				dateResult[k++] = dateArr[j];
				
				if(k>=count) break;
			}
		}
		
		return dateResult;
	}

	/**
	 * ȡ����������֮������ڣ���String���鷵�ء�
	 * @param startDate String����ʼ���ڡ�
	 * @param endDate   String����ֹ���ڡ�
	 * @return String[]���������顣
	 */
	public static String[] getBetweenDay(String startDate,String endDate){
		SimpleDateFormat df=new SimpleDateFormat ("yyyy-MM-dd"); 		
		Date start=format(startDate);
		Date end=format(endDate);
		List list=new ArrayList();
		while(start.before(end)||start.equals(end)){
			list.add(start);
			start=format(getNextDate(start,1));
		}
		int size=list.size();
		String[] reDate=new String[size];
		for(int i=0;i<size;i++){
			reDate[i]=df.format((Date)list.get(i));
		}
		return reDate;
	}
	
	/**
	 * ���ָ������ǰ�ƻ����xWeek���ܣ�����ȡ���ܵ�����һ�����������ڡ�
	 * @param strDate Sring��ָ�����ڡ�
	 * @param xWeek int��ǰ�ƻ���Ƶ�����
	 * @return �õ��ƶ��ܵ�����һ�������졣
	 */
	public static String[] get2WeekDay(String strDate, int xWeek){
		Date[] dateArr = get2WeekDay(format(strDate), xWeek);
		
		String strDateArr[] = new String[2];
		strDateArr[0] = format(dateArr[0]);
		strDateArr[1] = format(dateArr[1]);
		
		return strDateArr;
	}
	
	/**
	 * ���ָ������ǰ�ƻ����xWeek���ܣ�����ȡ���ܵ�����һ�����������ڡ�
	 * @param date java.util.Date��ָ�����ڡ�
	 * @param xWeek int��ǰ�ƻ���Ƶ�����
	 * @return �õ��ƶ��ܵ�����һ�������졣
	 */
	public static Date[] get2WeekDay(Date date, int xWeek){
		
		Map map = new HashMap();
		map.put("����һ", String.valueOf(1));
		map.put("���ڶ�", String.valueOf(2));
		map.put("������", String.valueOf(3));
		map.put("������", String.valueOf(4));
		map.put("������", String.valueOf(5));
		map.put("������", String.valueOf(6));
		map.put("������", String.valueOf(7));
		
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(date);
		
		SimpleDateFormat weekFormat = new SimpleDateFormat("EEEE");
		String week = weekFormat.format(date);
		
		Date[] dateArr = new Date[2];
		
		/*gc.add(1,-1)��ʾ��ݼ�һ.
		 *gc.add(2,-1)��ʾ�·ݼ�һ.
		 *gc.add(3.-1)��ʾ�ܼ�һ.
		 *gc.add(5,-1)��ʾ���һ.*/
		
		gc.add(5, -Integer.parseInt(String.valueOf(map.get(week))));		
		gc.add(3, xWeek);
		
		dateArr[0] = gc.getTime();
		
		gc.add(5, 6);		
		dateArr[1] = gc.getTime();
		
		return dateArr;
	}
	

	/**
	 * debug
	 */
	public static void main(String[] args) {
		try {
			// System.out.println(getPreWeekDate(6));
			// System.out.println(getIntevalDays("2005-01-01", "2005-01-03"));
			// System.out.println(getLastDay("2006", "01"));
			/*long start = getCurrDateTimeStr(parse("2006-12-5 6:00:00",
					YYYY_MM_DD_HH_MM_SS));
			long end = getCurrDateTimeStr(parse("2006-12-5 21:00:00",
					YYYY_MM_DD_HH_MM_SS));*/
			// long dif = end - start;
			// long result = dif / (60 * 60 * 1000);
			// System.out.println(start);
			// System.out.println(end);
			// System.out.println(dif);
			// System.out.println(result);
			/*long days = getIntevalDays("2006-12-01", "2006-12-31");*/
			
			String[] arr = get2WeekDay("2007-04-01",1);
			
			System.out.println(arr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * �õ��ƶ��ꡢ�µ����һ�졣
	 * @param year String����
	 * @param month String����
	 * @return �������һ�졣
	 */
	public static String getLastDay(String year, String month) {
		String day;
		day = String.valueOf(getLastDayOfMonth(year, Integer.parseInt(month)));
		return day;
	}

	/**
	 * �õ���ǰ�µ����һ�졣
	 * @return �������һ��
	 */
	public static String getLastDayOfMonth() {
		String day;
		day = String.valueOf(getLastDayOfMonth(Integer.parseInt(getMonth())));
		return day;
	}

	/**
	 * �õ��ƶ��ꡢ�µ����һ�졣
	 * @param year String����
	 * @param numMonth int���� 
	 * @return ���һ�졣
	 */
	public static int getLastDayOfMonth(String year, int numMonth) {
		int lastDay = 0;
		int thisYear = Integer.parseInt(year);
		if (numMonth < 8) {
			if (numMonth % 2 == 1)
				lastDay = 31;
			else if (numMonth == 2) {
				// if (thisYear == 0)
				// thisYear = getThisYear();
				if (((thisYear % 4 == 0) && (thisYear % 100 != 0))
						|| (thisYear % 400 == 0))
					lastDay = 29;
				else
					lastDay = 28;
			} else
				lastDay = 30;
		} else {
			if (numMonth % 2 == 1)
				lastDay = 30;
			else
				lastDay = 31;
		}
		return lastDay;
	}

	/**
	 * �õ���ǰ�ָ꣬���µ����һ�졣
	 * @param numMonth int��ָ����
	 * @return ���һ��
	 */
	public static int getLastDayOfMonth(int numMonth) {
		int lastDay = 0;
		int thisYear = 0;
		if (numMonth < 8) {
			if (numMonth % 2 == 1)
				lastDay = 31;
			else if (numMonth == 2) {
				if (thisYear == 0)
					thisYear = getThisYear();
				if (((thisYear % 4 == 0) && (thisYear % 100 != 0))
						|| (thisYear % 400 == 0))
					lastDay = 29;
				else
					lastDay = 28;
			} else
				lastDay = 30;
		} else {
			if (numMonth % 2 == 1)
				lastDay = 30;
			else
				lastDay = 31;
		}

		return lastDay;

	}

	/**
	 * �õ���ǰ��
	 * @return
	 */
	public static int getThisYear() {
		Calendar c = Calendar.getInstance();
		return c.get(Calendar.YEAR);
	}
	
	
	//���� �������Ϣ�õ����ܵĿ�ʼ����������
	
	/**
	* �õ�ĳ��ĳ�ܵĵ�һ��
	* @author Pu Xiongwei
	* @create-date:2007-9-24
	* @param year 
	* @param week
	* @return
	*/
	public static Date getFirstDayOfWeek(int year, int week) {
	Calendar c = new GregorianCalendar();
	c.set(Calendar.YEAR, year);
	c.set (Calendar.MONTH, Calendar.JANUARY);
	c.set(Calendar.DATE, 1);
	c.add(Calendar.DATE, (week-1) * 7);
	return getFirstDayOfWeek(c.getTime());
	}

	/**
	* �õ�ĳ��ĳ�ܵ����һ��
	* @author Pu Xiongwei
 	* @create-date:2007-9-24 
	* @param year
	* @param week
	* @return
	*/
	public static Date getLastDayOfWeek(int year, int week) {
	Calendar c = new GregorianCalendar(); 
	c.set(Calendar.YEAR, year);
	c.set(Calendar.MONTH, Calendar.JANUARY);
	c.set(Calendar.DATE, 1);
	c.add(Calendar.DATE , (week-1) * 7);
	return getLastDayOfWeek(c.getTime());
	}
	
	/**
	* ȡ�õ�ǰ���������ܵĵ�һ��
	* @author Pu Xiongwei
 	* @create-date:2007-9-24 
	* @param date
	* @return
	*/
	public static Date getFirstDayOfWeek(Date date) { 
	Calendar c = new GregorianCalendar();
	c.setFirstDayOfWeek(Calendar.MONDAY);
	c.setTime(date);
	c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
	return c.getTime ();
	}

	/**
	* ȡ�õ�ǰ���������ܵ����һ��
	* @author Pu Xiongwei
 	* @create-date:2007-9-24 
	* @param date
	* @return
	*/
	public static Date getLastDayOfWeek(Date date) {
	Calendar c = new GregorianCalendar();
	c.setFirstDayOfWeek(Calendar.MONDAY);
	c.setTime(date);
	c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
	return c.getTime();
	}

	public static int getYearCountBetween(Date date1, Date date2) {
		Calendar c1 = Calendar.getInstance();
		Calendar c2= Calendar.getInstance();
		c1.setTime(date1);
		c2.setTime(date2);
		int year = c2.get(Calendar.YEAR)-c1.get(Calendar.YEAR);
		return year>=0?year:-year;
	}
	
}
